package com.ibix.netty.communion;


import com.ibix.gui.component.MessageFrame;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/1/24.
 */
public class MyChannelFutureListener implements ChannelFutureListener {
    private MessageFrame messageFrame;


    public MyChannelFutureListener(MessageFrame messageFrame)
    {
        this.messageFrame = messageFrame;
    }

    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        if(channelFuture.isSuccess()){
            System.out.println("重新连接服务器成功");
            messageFrame.setVisible(false);
        }else{
            System.out.println("重新连接服务器失败");
            //3秒后重连
            channelFuture.channel().eventLoop().schedule(new Runnable() {
                @Override
                public void run() {
                    MyClient.doConnect();
                }
            },3, TimeUnit.SECONDS);
        }
    }
}
